package com.le.biz;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.le.bean.Topic;
import com.le.dao.TopicDao;
import com.le.web.model.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(
        propagation = Propagation.SUPPORTS, // 增删改必须要求事务REQUIRED， SUPPORTS是表示支持事务，但不必须
        isolation = Isolation.DEFAULT,
        timeout = 2000,
        readOnly = true,
        rollbackFor = RuntimeException.class
)
@Slf4j
public class TopicBizImpl implements TopicBiz{
    @Autowired
    private TopicDao topicDao;

    @Override
    public PageBean<Topic> findByPage(int pageno, int pagesize, int boardid) {
        int start = (pageno - 1) * pagesize; // 分页的起始位置

        List<Topic> list = this.topicDao.findByPage(start, pagesize, boardid);
        // 包装分页Bean
        PageBean topicPage = new PageBean();
        topicPage.setPageno(pageno);
        topicPage.setPagesize(pagesize);
        topicPage.setDataset(list);
        // 查询boardid下总的主题的数量
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("boardid", boardid);
        Integer total = this.topicDao.selectCount(wrapper);
        topicPage.setTotal(total);
        // 计算总页数   ： 总记录数/pagesize
        int totalpages = total%pagesize==0? total/pagesize: total/pagesize+1;
        topicPage.setTotalpages(totalpages);

        if (topicPage.getPageno() <= 1){
            topicPage.setPre(1);
        } else {
            topicPage.setPre(topicPage.getPageno() - 1);
        }
        // 下一页的页号
        if (topicPage.getPageno() == totalpages){
            topicPage.setNext(totalpages);
        } else {
            topicPage.setNext(topicPage.getPageno() + 1);
        }
        return topicPage;
    }

    @Override
    public Topic findById(Integer topicid) {
        return this.topicDao.findTopicById(topicid);
    }

    @Transactional(
            propagation = Propagation.REQUIRED, // 增删改必须要求事务REQUIRED， SUPPORTS是表示支持事务，但不必须
            isolation = Isolation.DEFAULT,
            timeout = 2000,
            rollbackFor = RuntimeException.class
    )
    @Override
    public void addTopic(Topic topic) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
        String format = sdf.format(date);
        topic.setPublishtime(format);
        topic.setModifytime(format);
        this.topicDao.insert(topic);
    }

    @Transactional(
            propagation = Propagation.REQUIRED, // 增删改必须要求事务REQUIRED， SUPPORTS是表示支持事务，但不必须
            isolation = Isolation.DEFAULT,
            timeout = 2000,
            rollbackFor = RuntimeException.class
    )
    @Override
    public void deleteTopicById(Integer topicid) {
        this.topicDao.deleteById(topicid);
    }

    @Transactional(
            propagation = Propagation.REQUIRED, // 增删改必须要求事务REQUIRED， SUPPORTS是表示支持事务，但不必须
            isolation = Isolation.DEFAULT,
            timeout = 2000,
            rollbackFor = RuntimeException.class
    )
    @Override
    public void updateTopic(Topic topic) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
        String format = sdf.format(date);
        topic.setModifytime(format);
        this.topicDao.updateById(topic);
    }
}
